package main

import (
  "github.com/gin-contrib/cors"
  "github.com/gin-gonic/gin"
  "novelDownlaod/download"
  "novelDownlaod/model"
  "novelDownlaod/search"
)
func main()  {
  gin.SetMode(gin.ReleaseMode)
  r := gin.Default()
  r.Use(cors.Default())

  r.GET("/search", func(context *gin.Context) {
   keyword:=context.Query("keyword")
   list,err := search.Search(keyword)
   if err!=nil {
     context.JSON(200,gin.H{
       "code":500,
     })
   }
   if len(list)==0 {
     list,err:=search.Search2(keyword)
     if err!=nil {
       context.JSON(200,gin.H{
         "code":500,
       })
     }
     context.JSON(200,gin.H{
       "code":200,
       "result":list,
     })
   }else{
     context.JSON(200,gin.H{
       "code":200,
       "result":list,
     })
   }


  })
  r.POST("/select", func(context *gin.Context) {
   selectBook :=model.Select{}
   context.BindJSON(&selectBook)
   if selectBook.Type==1 {
     url,err:=download.GetDownloadUrl(selectBook)
     if err!=nil {
       context.JSON(200,gin.H{
         "code":500,
       })
     }else{
       context.JSON(200,gin.H{
         "result":url,
       })
     }

   }else{
     url,err:=download.GetDownloadUrl2(selectBook)
     if err!=nil {
       context.JSON(200,gin.H{
         "code":500,
       })
     }else{
       context.JSON(200,gin.H{
         "result":url,
       })
     }
   }

   //config.BookName=selectBook.BookName
   //if url !="" {
   //  download.Download(url)
   //}
  })
  r.Run()
}
